package titt.head;

import java.util.HashMap;
import java.util.Map;

public class Head implements HTTPHead {

	private final String status;
	private final Header[] headers;
	private final Map<String, Header> hmap = new HashMap<String, Header>();	
	
	public Head(String status, Header... headers) {
		this.status = status;
		this.headers = headers;
		for(Header h : headers)
			hmap.put(h.getName(), h);
	}
		
	@Override
	public Header getHeader(String name) {
		return hmap.get(name);
	}
	
	@Override
	public Header[] getAllHeaders() {
		return headers;
	}

	@Override
	public String getStatusLine() {
		return status;
	}
	
	@Override
	public boolean equals(Object o) {
		if(!(o instanceof HTTPHead)) return false;
		if(!((HTTPHead)o).getStatusLine().equals(getStatusLine())) return false;
		for(Header h : ((HTTPHead)o).getAllHeaders())
			if(getHeader(h.getName()) == null || !getHeader(h.getName()).equals(h))
				return false;
		return true;
	}
	
}
